<?php

namespace App\Http\Controllers\V1\API;

use App\Http\Controllers\V1\API\BaseController;
use App\Models\V1\ApplyPays;
use App\Models\V1\NewStudent;
use App\Models\V1\Pays;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Validator;
use Log;

class ApplyPaysController extends BaseController
{
    /****
     * 费用列表接口
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function paysData(Request $request) {
        $code = $request->get('userID');
        $result = array(
            'errorCode' => Response::HTTP_NOT_FOUND,
            'errorMsg' => Response::$statusTexts[Response::HTTP_NOT_FOUND],
            'data' => array()
        );
        $error = $this->validatePay($request, $result);
        if (!empty($error) || isset($error)) {
            $result = $error;
            return $this->sendResponse($result, $result['errorCode']);
        }

        $payList = $this->getPaysList($request->get('userID'));
        if ($payList->isEmpty()) {
            $result['errorCode'] = Response::HTTP_OK;
            $result['errorMsg'] = Response::$statusTexts[Response::HTTP_NO_CONTENT];
        }else {
            // 格式化数据
            $pays = array();
            $pays['isSubmit'] = $this->isPay($code);
            $formatPayList = array();
            foreach ($payList as $item) {
                $formatPay = array();
                $formatPay['payCode'] = $item['code'];
                $formatPay['payName'] = $item['name'];
                $formatPay['payRemark'] = isset($item['remark'])?$item['remark']:'';
                $formatPay['payAmount'] = $item['amount'];
                $formatPay['unit'] = $item['unit'];
                $formatPay['type'] = isset($item['type'])?$item['type']:'';
                $formatPay['sub_amount'] = $item['sub_amount'];
                array_push($formatPayList, $formatPay);
            }
            $pays['payList'] = $formatPayList;
            $result['errorCode'] = Response::HTTP_OK;
            $result['errorMsg'] = Response::$statusTexts[Response::HTTP_OK];
            $result['data'] = $pays;
        }
        return $this->sendResponse($result, $result['errorCode']);
    }

    /****
     * 获取所有支付项
     * @return \Illuminate\Support\Collection
     */
    private function getPaysList($code)
    {
        $arrival_code = $this->getArrivalCode($code);
        $data = Pays::where('arrival_code',$arrival_code)->orderBy('id','asc')->get();
        return $data;
    }

    /***
     * 获取当前的迎新代码
     * @return array
     */
    private function getArrivalCode($code)
    {
        $student = NewStudent::where('exam_code',$code)->first();
        return $student['arrival_code'];
    }

    /***
     * 用户是否缴费
     * @param $code
     */
    private function isPay($code){
        $pay = ApplyPays::where(['exam_code'=>$code,'status'=>true])->first();
        if (!empty($pay) || isset($pay)) {
            return true;
        }
        return false;
    }

    /***
     * 验证参数
     * @param Request $request
     * @return mixed
     */
    public function validatePay(Request $request)
    {
        //校验规则
        $rules = array();
        $rules['userID'] = 'required';
        $rules['userID'] = 'required|exists:basic_new_students,exam_code,deleted_at,NULL|bail';

        //错误提示信息
        $messages = array();
        $messages['userID.required'] = '用户代码不能为空';
        $messages['userID.exists'] = '用户不存在';
        $validator = Validator::make($request->all(),$rules,$messages);  //第一个参数是提交的数据，第二个是校对规则，第三个是错误信息
        $errors = $validator->errors()->all(); //这是捕获到的所有参数验证的错误信息
        if(!empty($errors)) {
            $first_errors = $errors[0];
            $result['errorCode'] = Response::HTTP_BAD_REQUEST;
            $result['errorMsg'] = $first_errors;
            return $result;
        }
    }
}
